import { withRouter } from 'next/router'
import { getID } from '@/utils'
// 组件
import List from '@/pages_components/pathPage/GuideList';
import Detail from '@/pages_components/pathPage/GuideDetail';
// ssr api
import { productList_SSR, productDetail_SSR, guideMax_New } from '@/ssr';

const PathPage = ({ router, list, list_total, detail, detail_data, newList }) => {
    const { asPath } = router;
    // 渲染列表页，第二页开始
    if(asPath.includes('/list_')){
        return <List router={router} list={list} total={list_total} />
    // 渲染详情
    }else {
        return <Detail router={router} ssr={detail} ssr_data={detail_data} newList={newList} />
    }
}

// ssr 渲染
PathPage.getInitialProps = async (res) => {

    const { asPath, query, query: { path } } = res;
    // 返回列表数据
    if(asPath.includes('/list_')){
        const _query = query;
        delete _query.path;
        const { 
            ssr: list, 
            total: list_total
        } = await productList_SSR('/guide', { pathname: path, query: _query });
        return { list, list_total }

    // 返回详情数据
    }else{
        
        const { 
            ssr: detail, 
            ssr_data: detail_data
        } = await productDetail_SSR('/guide/', getID(path));
        // 最新攻略（新增）
        const newList = [];
        const { data } = await guideMax_New(1, 3);
        if(data?.code === 0 && data?.data?.records){
            newList.push(...data.data.records)
        }

        return { detail, detail_data, newList }
    }
}

export default withRouter(PathPage)